﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using Abp.AspNetCore.Mvc.Authorization;
using Abp.UI;
using Microsoft.AspNetCore.Mvc;
using Partner.Surround.Controllers;
using Partner.Surround.Base.DataDictionaries;
using Partner.Surround.Base.DataDictionaries.Dto;
using Partner.Surround.Web.Models.Common;
using Partner.Surround.Web.Mvc.Areas.Base.Models.DataDictionaries;

namespace Partner.Surround.Web.Mvc.Areas.Base.Controllers
{
    /// <summary>
    /// 数据字典控制器
    /// </summary>
    [Area("Base")]
    [AbpMvcAuthorize]
    public class DataDictionaryController : SurroundControllerBase
    {
        #region 初始化
        private readonly IDataDictionaryAppService _dataDictionaryAppService;

        public DataDictionaryController(IDataDictionaryAppService dataDictionaryAppService)
        {
            _dataDictionaryAppService = dataDictionaryAppService;
        }
        #endregion

        #region 数据字典
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取数据字典列表
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> GetDataDictionaryList()
        {
            var dataDictionaryListDto = await _dataDictionaryAppService.GetAllDataDictionary();
            return Json(new ResponseParamListViewModel<DataDictionaryListDto>(dataDictionaryListDto.Items));
        }

        /// <summary>
        /// 创建或更新数据字典
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        public async Task<IActionResult> CreateOrUpdateDataDictionary(DataDictionaryRequestViewModel viewModel)
        {
            var output = await _dataDictionaryAppService.GetDataDictionaryForEdit(new NullableIdDto<long>(viewModel.DataDictionaryId));
            return View(output);
        }

        /// <summary>
        /// 创建或更新数据字典
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> CreateOrUpdateDataDictionary([FromBody]CreateOrUpdateDataDictionaryInput input)
        {
            await _dataDictionaryAppService.CreateOrUpdateDataDictionary(input);

            return Json(new ResponseParamViewModel(input.DataDictionary.Id.HasValue ? L("更新字典类型成功") : L("新增字典类型成功")));
        }

        /// <summary>
        /// 删除数据字典
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> DeleteDataDictionary([FromBody]List<EntityDto<long>> input)
        {
            await _dataDictionaryAppService.DeleteDataDictionary(input);

            return Json(new ResponseParamViewModel(L("删除字典类型成功")));
        }
        #endregion

        #region 数据字典项
        /// <summary>
        /// 获取数据字典项列表
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        public async Task<JsonResult> GetDataDictionaryItemList(DataDictionaryItemRequestViewModel viewModel)
        {
            var dataDictionaryItemListDto = await _dataDictionaryAppService.GetAllDataDictionaryItem(new GetAllDataDictionaryItemInput()
            {
                DataDictionaryId = viewModel.DataDictionaryId.Value
            });

            return Json(new ResponseParamListViewModel<DataDictionaryItemListDto>(dataDictionaryItemListDto.Items));
        }

        /// <summary>
        /// 根据字典类型获取字典项
        /// </summary>
        /// <param name="type">字典类型</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> GetDataDictionaryItemListByTypeNames([FromBody]GetDataDictionaryListByTypeNamesInput input)
        {
            var dataDictionaryItemList = await _dataDictionaryAppService.GetDataDictionaryListByTypeNames(input);
            return Json(new ResponseParamListViewModel<DataDictionaryListDto>(dataDictionaryItemList.Items));
        }

        /// <summary>
        /// 创建或更新数据字典项
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        public async Task<IActionResult> CreateOrUpdateDataDictionaryItem(DataDictionaryItemRequestViewModel viewModel)
        {
            if (!viewModel.DataDictionaryId.HasValue && !viewModel.DataDictionaryItemId.HasValue)
            {
                throw new UserFriendlyException(L("请求参数有误"));
            }

            var output = await _dataDictionaryAppService.GetDataDictionaryItemForEdit(new NullableIdDto<long>(viewModel.DataDictionaryItemId));

            if (!viewModel.DataDictionaryItemId.HasValue)
            {
                output.DataDictionaryItem.DataDictionaryId = viewModel.DataDictionaryId.Value;
            }

            return View(output);
        }

        /// <summary>
        /// 创建或更新数据字典项
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> CreateOrUpdateDataDictionaryItem([FromBody]CreateOrUpdateDataDictionaryItemInput input)
        {
            await _dataDictionaryAppService.CreateOrUpdateDataDictionaryItem(input);

            return Json(new ResponseParamViewModel(input.DataDictionaryItem.Id.HasValue ? L("更新字典项成功") : L("新增字典项成功")));
        }

        /// <summary>
        /// 删除数据字典项
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> DeleteDataDictionaryItem([FromBody]List<EntityDto<long>> input)
        {
            await _dataDictionaryAppService.DeleteDataDictionaryItem(input);

            return Json(new ResponseParamViewModel(L("删除字典项成功")));
        }
        #endregion
    }
}